package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    /*
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional

    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        log.info("套餐信息：{}",setmeal);

        //将相应的数据添加到套餐表中
        setmealMapper.save(setmeal);
        //获取套餐的id
        Long setmealId = setmeal.getId();



        List<SetmealDish> setmealDishList=setmealDTO.getSetmealDishes();
        setmealDishList.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
                }

        );
        log.info("");
        for (SetmealDish setmealDish : setmealDishList) {
            setmealDishMapper.save(setmealDish);

        }




    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmealVOPage=setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(setmealVOPage.getTotal(),setmealVOPage.getResult());
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Transactional
    public void delete(Integer[] ids) {
        //1.删除套餐中的信息
        for (Integer id : ids) {
            setmealMapper.delete(id);
            //2.删除套餐菜品中的信息
            setmealDishMapper.deleteBySetmealId(id);
        }


    }

    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    @Transactional
    public SetmealVO getById(Integer id) {
        SetmealVO setmealVO = new SetmealVO();

        Setmeal setmeal=setmealMapper.getById(id);
        BeanUtils.copyProperties(setmeal,setmealVO);


        List<SetmealDish> setmealDishList=setmealDishMapper.getDishsBySetmealId(id);
        log.info("菜品信息：{}",setmealDishList);
        setmealVO.setSetmealDishes(setmealDishList);




        return setmealVO;
    }

    /**
     * 更新套餐
     * @param setmealDTO
     */
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //1.先删除信息
        Long setmealDTOId = setmealDTO.getId();
        setmealMapper.delete(Math.toIntExact(setmealDTOId));

        setmealDishMapper.deleteBySetmealId(Math.toIntExact(setmealDTOId));


        //2.在插入信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.save(setmeal);
        //获得套餐id
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDishMapper.save(setmealDish);
        }


    }

    /**
     * 修改套餐的状态
     * @param id
     * @param status
     */
    public void status(Integer id, Integer status) {
        setmealMapper.status(id,status);



    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
